#-*- encoding:utf-8 -*-
from __future__ import print_function

import sys
try:
    reload(sys)
    sys.setdefaultencoding('utf-8')
except:
    pass

import codecs
from textrank4zh import TextRank4Keyword, TextRank4Sentence

#text = codecs.open('../test/doc/talk.txt', 'r', 'utf-8').read()
top_num = int(sys.argv[1])

input = sys.argv[2:]
#print(input)
text = ''.join(input)
tr4w = TextRank4Keyword()

tr4w.analyze(text=text, lower=True, window=2)   # py2中text必须是utf8编码的str或者unicode对象，py3中必须是utf8编码的bytes或者str对象


#
# print( '关键词：' )
# result += '关键词&权值：' +'\n'
# i = 0
# for item in tr4w.get_keywords(20, word_min_len=1):
#     print(item.word, item.weight)
#     i += 1
#     result += str(i) + '.' + item.word + '：' + str(item.weight) + '\n'

# print()
# print( '关键短语：' )
# result += '\n'
# result += '关键短语：' +'\n'
# j = 0
# for phrase in tr4w.get_keyphrases(keywords_num=20, min_occur_num= 2):
#     print(phrase)
#     j += 1
#     result += str(j) + '.' + phrase + '\n'

tr4s = TextRank4Sentence()
tr4s.analyze(text=text, lower=True, source = 'all_filters')

# 记录抽取的关键信息和摘要
result = ''
# 对抽取的句子按文本语序进行排序
summary=[]

for j in range(1,1000):
    summary.insert(j,0)

index=0
sentence = ''
for item in tr4s.get_key_sentences(num=top_num):
  #  print(item.index, item.weight, item.sentence)
    
    index=item.index
    sentence=item.sentence
    summary.insert(index,sentence)

print( '####' )
result += '摘要：' +'\n\n'
k = 0
for i in range(len(summary)):
    if summary[i]:
       print(summary[i])
       k += 1
       # result += str(k) + '.' + str(item.index) + item.sentence + ' ' + ' ' + str(item.weight) + '\n'
       result += str(k) + '.' + summary[i] + '\n\n'


#print('\n')
#print('抽取结果写入文件......')

# 将关键词，关键短语和摘要写入txt文件中
#file_name = './result/result.txt'
#talk_file = open(file_name, 'w', encoding='utf-8')
#talk_file.write(result)
#talk_file.close()

#print('\n')
#print('抽取完成！')
